<?php
/**
* Model Class
* 
* @author Guillermo Landín
* @link 
*/ 

class Glossarymodel extends Model {

    function __construct()
    {
        parent::Model();
        
        $this->CI =& get_instance(); 
    }
    
    public function get_glossary_of_the_day(){
    
         $this->db->select('Title_Glossary, Introduction_Glossary, id_Glossary')
                ->from('glossary')
                ->where('Active_Glossary', 1)
                ->order_by('RAND()')
                ->limit(1);            

        $query = $this->db->get();

        return $query->row_array();
    
    }

    public function get_popular_glossary_items(){
    
         $this->db->select('Title_Glossary AS title, Introduction_Glossary AS text, id_Glossary AS id')
                ->from('glossary')
                ->where('Active_Glossary', 1)
                ->order_by('RAND()')
                ->limit(6);            

        $query = $this->db->get();

        return $query->result_array();
    
    }

    public function get_item($id){

         $this->db->select('Title_Glossary, Introduction_Glossary, Description_Glossary, id_Glossary, Image_Glossary')
                ->from('glossary')                                                           
                ->where('Active_Glossary', 1)
                ->where('id_Glossary', $id)
                ->limit(1);            

        $query = $this->db->get();

        return $query->row_array();
    
    }

    public function get_list_by_letter($letter = FALSE){
    
         $this->db->select('Title_Glossary AS title, Introduction_Glossary AS text, id_Glossary AS id')
                ->from('glossary')
                ->where('Active_Glossary', 1)
                ->where('Title_Glossary LIKE', $letter.'%')
                ->order_by('Title_Glossary', 'ASC');

        $query = $this->db->get();

        return $query->result_array();
    
    }

    public function get_alphabet(){
    
        return array_merge(range('a','z'));

    }

    public function get_all_items(){
    
         $this->db->select('Title_Glossary AS title, id_Glossary AS id')
                ->from('glossary')
                ->where('Active_Glossary', 1);            

        $query = $this->db->get();

        return $query->result_array();
    
    }

    public function link_glossary_terms($str='', $disable_link = false)
    {

        $terms = $this->get_all_items();
        $str = ' '.$str.' ';
        $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';

        foreach ($terms as $term){
            if($disable_link == false) $link = site_url('glossary/view/'.$term['id']);
            else $link = "#";
            $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($term['title'], '/')).")({$delim})/i", "\\1<a href=\"$link\">\\2</a>\\3", $str);
        }        

        return trim($str);
    }

    /***********************************   
    *   Sub-menu functions
    ***********************************/

    public function get_submenu(){

        $alphabet = $this->get_alphabet();    

        foreach($alphabet as &$row){
            $row = array( 
                        'name' => $row, 
                        'controller' => 'glossary/letter/'.$row, 
                        'param' => $row
                        );
        }

        return $alphabet;    

    }

    public function get_submenu_by_letter($letter = FALSE){

        //get the first level submenu items
        $submenu = $this->get_submenu();

        //get the items for the selected letter      
        $items = $this->get_submenu_items_by_letter($letter); 

         //insert array of items into submenu array
        foreach($submenu as &$item){
            if ( $item['param'] == $letter ) {

                $item['selected'] = TRUE;
                $item['content'] =  $items;
            
            }
        }

        return $submenu;

    }

    public function get_submenu_by_id($id = FALSE){

        //get the first letter for item $id      
        $item = $this->get_item($id);
        $letter = strtolower(substr($item['Title_Glossary'], 0,1));

        //get the items for the selected letter      
        $submenu = $this->get_submenu_by_letter($letter); 

        //set item as selected
        foreach($submenu as &$letter_row){
            if (isset($letter_row['content'])) {
                foreach($letter_row['content'] as &$letter_items) {
                    if ($letter_items['param'] == $id) $letter_items['selected'] = TRUE;
                }
            }
        }

        return $submenu;

    }

    //returns an array of items for a given letter with fields 'name', 'controller' and 'param'
    private function get_submenu_items_by_letter($letter = FALSE){

        $items = $this->get_list_by_letter($letter);

        foreach($items as &$row){
            $row = array( 
                            'name' => $row['title'], 
                            'controller' => 'glossary/view/'.$row['id'], 
                            'param' => $row['id']
                            );
        }

        return $items;

    }
 


/***********************************
*   Backoffice functions
***********************************/

    //returns the list of glossary
    function admin_get_list($fields='*'){
    
        $this->db->select($fields)
                ->from('glossary')
                ->order_by('Title_Glossary');  

        $query = $this->db->get();

        $result = $query->result_array();
                                 
        return $result;

    }  
    public function admin_get_item($id=FALSE, $fields='*'){

        $this->db->select($fields)
                ->from('glossary')       
                ->where('id_Glossary', $id);  
        
        $query = $this->db->get();
        return $query->row_array();

    }  
    public function admin_update($id=FALSE, $data=array()){

        $this->db->where('id_Glossary', $id)
                ->update('glossary', $data); 

    }                      
    public function admin_insert($data=array()){

        $data["id_Glossary"] = uniqid();
        $data["Active_Glossary"] = 0;
        $this->db->insert('glossary', $data); 
        return $data["id_Glossary"];

    }  
    public function admin_delete($id=FALSE){

        $this->db->where('id_Glossary', $id);
        $this->db->delete('glossary'); 

    } 
    public function is_duplicated($field, $value, $id=FALSE)
    {
    
        $this->db->select('count(1) as counter')
                ->from('glossary')
                ->where($field, $value);        
        if($id!==FALSE) $this->db->where('id_Glossary !=', $id);
    
        $query = $this->db->get();
        $row = $query->row_array();
        if($row == NULL)
            return FALSE;
        
        if($row["counter"] > 0){
            return TRUE;
        }else{
            return FALSE;
        }

    } 


}
    
    


/* End of file newsmodel.php */
/* Location: ./system/application/models/newsmodel.php */ 
